package cc.wfu.trigger.job;

import cc.wfu.domain.order.service.IProductOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 超时关单任务
 */
@Slf4j
@Component()
public class TimeoutCloseOrderJob {

    @Resource
    private IProductOrder productOrder;

    /**
     * 每 5 分钟执行一次
     * 超时关单任务
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void exec() {
        try {
            // 查询超时15分钟，未支付订单
            List<String> orderIds = productOrder.queryTimeoutCloseOrderList();
            if (orderIds == null || orderIds.isEmpty()) {
                log.info("定时任务，超时15分钟订单关闭，暂无超时未支付订单 orderIds is empty");
                return;
            }
            for (String orderId : orderIds) {
                // 变更；订单支付关闭
                boolean status = productOrder.changeOrderClose(orderId);

                log.info("定时任务，超时15分钟订单关闭 orderId: {} status: {}", orderId, status);
            }
        } catch (Exception e) {
            log.error("定时任务，超时15分钟订单关闭失败", e);
        }
    }

}
